/*
 * Copyright 2018 xwc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.xwc.view;

import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.Color;
import ohos.media.image.PixelMap;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

/**
 * Created by xwc on 2018/2/24.
 */
public abstract class ClipHelper {

    protected final Path mPath = new Path();
    //    private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final Paint paint = new Paint();

    /**
     * 设置剪辑路径
     *
     * @param width  图像宽
     * @param height 图像高
     */
    public void setUpClipPath(int width, int height) {
        mPath.reset();
        final Path clipPath = getClipPath(width, height);
        if (clipPath != null) {
            mPath.set(clipPath);
        }
    }

    /**
     * 获取剪辑路径
     *
     * @param width
     * @param height
     * @return Path
     */
    protected final Path getClipPath(int width, int height) {
        return createClipPath(width, height);
    }

    /**
     * 创建目标图像
     *
     * @param width  图像宽
     * @param height 图像高
     * @return PixelMap
     */
    public PixelMap createMask(int width, int height) {
        PixelMap.InitializationOptions initializationOptions= new PixelMap.InitializationOptions();
        Size size = new Size();
        size.width =width;
        size.height=height;
        initializationOptions.size = size;
        PixelMap pixelMap =PixelMap.create(initializationOptions);

        final Canvas canvas = new Canvas();
        PixelMapHolder holder=new PixelMapHolder(pixelMap);
        canvas.drawPixelMapHolder(holder,width,height,paint);
        canvas.drawPath(mPath, paint);
        return pixelMap;
    }

    /**
     * 创建图形返回图形path
     *
     * @param width  .
     * @param height .
     * @return Path .
     */
    abstract Path createClipPath(int width, int height);
}
